package com.qianfeng.tiantian_banjia;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.qianfeng.tiantian_banjia.adapter.MyListviewAdapter;
import com.qianfeng.tiantian_banjia.bean.Goods;
import com.qianfeng.tiantian_banjia.callback.CallBack;
import com.qianfeng.tiantian_banjia.fragment.TeJiaInfoActivity;
import com.qianfeng.tiantian_banjia.task.StringAsyncTask;
import com.qianfeng.tiantian_banjia.utils.AppContasnts;
import com.qianfeng.tiantian_banjia.utils.AsyncImageLoader;
import com.qianfeng.tiantian_banjia.utils.NetUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class AllSortActivity extends Activity {
	 ListView listview;
	private List<Goods> listAllGoods = new ArrayList<Goods>();
	private MyListviewAdapter listviewAdapter;
	AsyncImageLoader loader;
	private StringAsyncTask stringAsyncTask;
	PullToRefreshListView xListView;
	private LinearLayout layout;
	private TextView txtAllSort;
	String url = "http://api.gouwu.duba.com/mobile_category/get_category_product_list.json?root_category_id=22&uid=35290005700668130:39:26:a2:ec:9e&page=1&pagesize=10s";
	ActionBar bar;
	private Goods goods;
	int id;
	SharedPreferences shpref;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	
		bar = getActionBar();
		bar.hide();
		if (NetUtils.isConnection(getApplicationContext())) {
			setContentView(R.layout.allsort_activity);
			txtAllSort = (TextView) findViewById(R.id.txt_allsort_titleActivity);
			// getData(AppContasnts.getStringPath());
			Intent intent = getIntent();

			id = intent.getIntExtra("id", -1);
			connect();
			switch (id) {

			case 0:
				Log.i("123", "bundle.getStringArray(str);----case 0");

				getData(url);
				Log.i("123", "	getData(AppContasnts.getClouthesShoes());+---"
						+ url);
				
				break;
			case 1:
				txtAllSort.setText("服饰鞋包");
			

				getData(AppContasnts.getClouthesShoes());
				break;
			case 2:
				txtAllSort.setText("日用百货");
				getData(AppContasnts.getDayliyGoods());
				break;
			case 3:
				getData(AppContasnts.getBaojian());
				txtAllSort.setText("食品保健");
				break;
			case 4:
				getData(AppContasnts.getMeizhuang());
				txtAllSort.setText("美妆个护");
				break;
			case 5:
				txtAllSort.setText("母婴玩具");
				getData(AppContasnts.getMuyingPlay());
				break;
			default:
				break;
			}

		} else {
			setContentView(R.layout.allsort_activity);
			disconnect();

		}

	}

	private void disconnect() {
		txtAllSort = (TextView) findViewById(R.id.txt_allsort_titleActivity);
		Intent intent = getIntent();

		int id2 = intent.getIntExtra("id", -1);
		loader = new AsyncImageLoader(this);
		// xListView = (XListView)this.findViewById(R.id.xlistview_allsort);
		listviewAdapter = new MyListviewAdapter(this, listAllGoods, loader);
		xListView = (PullToRefreshListView) findViewById(R.id.allsort_pulllist);
		
		final View view = LayoutInflater.from(this).inflate(
				R.layout.network_connect, null);
		listview = xListView.getRefreshableView();

		listview .addHeaderView(view);

		xListView.setAdapter(listviewAdapter);
		switch (id2) {

		case 0:
			Log.i("123", "bundle.getStringArray(str);----case 0");

			xListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

				@Override
				public void onRefresh(PullToRefreshBase<ListView> refreshView) {
					// TODO Auto-generated method stub
					if (NetUtils.isConnection(getApplicationContext())) {
						// layout.setVisibility(View.GONE);
						ILoadingLayout iLoadingLayout=xListView.getLoadingLayoutProxy();
						iLoadingLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.default_ptr_flip));
						iLoadingLayout.setRefreshingLabel("努力为你加载页。。。。");
						xListView.setAdapter(listviewAdapter);
						getData(AppContasnts.getStringPath());
						listview .removeHeaderView(view);

					}

				}
			});
			break;
		case 1:
			txtAllSort.setText("服饰鞋包");
	

			xListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

				@Override
				public void onRefresh(PullToRefreshBase<ListView> refreshView) {
					// TODO Auto-generated method stub
					if (NetUtils.isConnection(getApplicationContext())) {
						// layout.setVisibility(View.GONE);
						ILoadingLayout iLoadingLayout=xListView.getLoadingLayoutProxy();
						iLoadingLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.default_ptr_flip));
						iLoadingLayout.setRefreshingLabel("努力为你加载页。。。。");
						xListView.setAdapter(listviewAdapter);
						getData(AppContasnts.getClouthesShoes());
						listview .removeHeaderView(view);

					}

				}
			});
			break;
		case 2:
			txtAllSort.setText("日用百货");
			xListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

				@Override
				public void onRefresh(PullToRefreshBase<ListView> refreshView) {
					// TODO Auto-generated method stub
					if (NetUtils.isConnection(getApplicationContext())) {
						// layout.setVisibility(View.GONE);
						ILoadingLayout iLoadingLayout=xListView.getLoadingLayoutProxy();
						iLoadingLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.default_ptr_flip));
						iLoadingLayout.setRefreshingLabel("努力为你加载页。。。。");
						xListView.setAdapter(listviewAdapter);
						getData(AppContasnts.getDayliyGoods());
						listview .removeHeaderView(view);

					}

				}
			});
			break;
		case 3:
			
			getData(AppContasnts.getBaojian());
		//	txtAllSort.setText("食品保健");
			break;
		case 4:
			//getData(AppContasnts.getMeizhuang());
			txtAllSort.setText("美妆个护");
			break;
		case 5:
			txtAllSort.setText("母婴玩具");
			//getData(AppContasnts.getMuyingPlay());
			break;
		default:
			break;
		}
		
		xListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				// TODO Auto-generated method stub
				if (NetUtils.isConnection(getApplicationContext())) {
					// layout.setVisibility(View.GONE);
					ILoadingLayout iLoadingLayout=xListView.getLoadingLayoutProxy();
					iLoadingLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.default_ptr_flip));
					iLoadingLayout.setRefreshingLabel("努力为你加载页。。。。");
					xListView.setAdapter(listviewAdapter);
					getData(AppContasnts.getStringPath());
					listview .removeHeaderView(view);

				}

			}
		});

	}

	public void btnClick(View v){
		switch (v.getId()) {
		case R.id.img_allsort_back:
			finish();
			break;

		default:
			break;
		}
	}
	public void connect() {
		// loader = new AsyncImageLoader(getActivity());
		loader = new AsyncImageLoader(this);
		// xListView = (XListView)this.findViewById(R.id.xlistview_allsort);
		listviewAdapter = new MyListviewAdapter(this, listAllGoods, loader);
		xListView = (PullToRefreshListView) this
				.findViewById(R.id.allsort_pulllist);
		xListView.setAdapter(listviewAdapter);

		xListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "提交陈宫",
						Toast.LENGTH_SHORT).show();

				Intent intent = new Intent();
				Goods goods = listAllGoods.get(position);
				Bundle bundle = new Bundle();
				bundle.putSerializable("goods", goods);
				intent.putExtra("bundle", bundle);
				intent.setClass(getApplicationContext(),
						TeJiaInfoActivity.class);

				startActivity(intent);
			}
		});
	}

	public void getData(String path) {

		// TODO Auto-generated method stub
		stringAsyncTask = new StringAsyncTask(new CallBack() {

			@Override
			public void getStringData(String data) {
				Toast.makeText(AllSortActivity.this, " 数据又返回...", 1).show();
				List<Goods> list = new ArrayList<Goods>();
				// TODO Auto-generated method stub
				if (!TextUtils.isEmpty(data)) {

					try {
						JSONObject jsonResult = new JSONObject(data);
						JSONArray jsonArray = jsonResult.optJSONArray("data");
						Log.i("123", "Jsoadrr" + jsonArray.toString());
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject goodsJson = jsonArray.optJSONObject(i);
							goods = new Goods();
							goods.paraseJsonData(goodsJson);
							list.add(goods);
							// id = goods.getUid();

						}
						listAllGoods.addAll(list);
						// Log.i("123", "listsize" + listGoods.size());
						listviewAdapter.notifyDataSetChanged();
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// Log.i("123", listGoods.size() + "");

				}
			}
		});

		stringAsyncTask.execute(path);

	}

	/*@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		stringAsyncTask.cancel(true);
	}
*/
}
